<?php
function imgCut($src_file,$new_width,$new_height,$start_x,$start_y) {
    $img_type=strtolower(pathinfo($src_file, PATHINFO_EXTENSION));
    switch ($img_type) {
        case 'jpg':
            if(@!($src_img= imagecreatefromjpeg($src_file))){
                if(@!($src_img = imagecreatefrompng($src_file))){
                    $src_img = imagecreatefromgif($src_file);
                }
            }
            break;
        case 'png':
            if(@!($src_img= imagecreatefrompng($src_file))){
                if(@!($src_img = imagecreatefromjpeg($src_file))){
                    $src_img = imagecreatefromgif($src_file);
                }
            }
            break;
        case 'gif':
            if(@!($src_img= imagecreatefromgif($src_file))){
                if(@!($src_img = imagecreatefrompng($src_file))){
                    $src_img = imagecreatefromjpeg($src_file);
                }
            }
            break;
        default:
            echo "载入图像错误!";
    }
    /* 获取源图片的宽度和高度 */
    $src_width = imagesx($src_img);
    $src_height = imagesy($src_img);

    // 为剪切图像创建背景画板
    $mid_img = imagecreatetruecolor($new_width, $new_height);
    //拷贝剪切的图像数据到画板，生成剪切图像
    imagecopy($mid_img, $src_img, 0, 0, $start_x, $start_y, $new_width, $new_height);
    // 为裁剪图像创建背景画板
    $new_img = imagecreatetruecolor($new_width, $new_height);
    //拷贝剪切图像到背景画板，并按比例裁剪
    imagecopyresampled($new_img, $mid_img, 0, 0, 0, 0, $new_width, $new_height,$new_width, $new_height);

    $outFile = './out';
    switch ($img_type) {
        case 'jpg':
            $outFile = $outFile.'.jpg';
            imagejpeg($new_img,$outFile  );
            break;
        case 'png':
            $outFile = $outFile.'.png';
            imagepng($new_img, $outFile );
            break;
        case 'gif':
            $outFile = $outFile.'.gif';
            imagepng($new_img, $outFile );
            break;
        default:
            break;
    }
    return ltrim($outFile, '.');
}
echo imgCut('./complex.jpg',100,100,0,0);
